﻿#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

// 枚举值表⽰颜⾊
enum Colour
{
	RED,
	BLACK
};

template <class T>
struct RBTreeNode
{
	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;

	RBTreeNode(const T& data)
		:_data(data)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{}
};

template<class T, class ref, class ptr>
struct RBTree_Iterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTree_Iterator<T, ref, ptr> Self;

	Node* _node;
	Node* _root; //增加根节点

	RBTree_Iterator(Node* node, Node* root)
		:_node(node)
		, _root(root)
	{}

	ref operator*()
	{
		return _node->_data;
	}

	ptr operator->()
	{
		return &_node->_data;
	}
	 
	bool operator!=(const Self& s) const
	{
		return _node != s._node;
	}

	bool operator==(const Self& s) const
	{
		return _node == s._node;
	}

	Self& operator++() //前置++
	{
		if (_node->_right) //右不为空
		{
			//右子树最左结点就是中序第⼀个
			Node* min = _node->_right;//先指向右树
			while (min->_left)
			{
				min = min->_left;//直接往左一直找
			}
			_node = min;
		}
		else //右不为空
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right) //parent可能会走到空
			{
				cur = parent;
				parent = parent->_parent;
			}
			//当cur不是parent的右子树时，就是parent的左子树，就是++要访问的下一个节点
			_node = parent; 
		}
		return *this;
	}

	Self& operator--()
	{
		if (_node == nullptr) // end()
		{
			// --end()，特殊处理，走到中序最后⼀个结点，整棵树的最右结点
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else if (_node->_left)
		{
			// 左子树不为空，中序左子树最后⼀个
			Node* rightMost = _node->_left;
			while (rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else
		{
			// 孩⼦是父亲右的那个祖先
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

};




template<class K, class T, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef typename RBTree_Iterator<T, T&, T*> Iterator;
	typedef typename RBTree_Iterator<T, const T&, const T*> const_Iterator;

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;  //找最左节点
		}
		return Iterator(cur, _root);
	}

	Iterator End()
	{
		return Iterator(nullptr, _root);
	}

	const_Iterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;  //找最左节点
		}
		return Iterator(cur, _root);
	}

	const_Iterator End() const
	{
		return Iterator(nullptr, _root);
	}

	pair<Iterator, bool> insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;  //根节点为黑色
			return make_pair(Iterator(_root, _root), true);
		}
		Node* parent = nullptr;
		Node* cur = _root;

		KeyOfT com; //仿函数

		while (cur)
		{
			if (com(cur->_data) > com(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (com(cur->_data) < com(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else 
				return make_pair(Iterator(cur, _root), false);
		}

		cur = new Node(data);
		Node* newnode = cur; //记录新插入的节点
		cur->_col = RED;  //新插入节点为红色
		if (com(cur->_data) < com(parent->_data))
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;


		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left) //u在右
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED) //u存在且为红
				{
					parent->_col = BLACK; //u和p变黑
					uncle->_col = BLACK;
					grandfather->_col = RED;//g变红

					cur = grandfather; //继续向上更新
					parent = cur->_parent;
				}
				else //u不存在 或 u存在且为黑
				{
					if (cur == parent->_left) //单旋
					{
						rotateR(grandfather);//以g为旋转点右旋
						parent->_col = BLACK;  //变色
						grandfather->_col = RED;
					}
					else //双旋
					{
						rotateL(parent); //先对p左旋
						rotateR(grandfather);//再对g右旋
						//变色
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else //u在左
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED) //u存在且为红
				{
					parent->_col = BLACK; //p和u变黑
					uncle->_col = BLACK;
					grandfather->_col = RED;//g变红

					cur = grandfather; //继续向上更新
					parent = cur->_parent;
				}
				else //u不存在 或 存在且为黑
				{
					if (cur == parent->_right) //单旋
					{
						rotateL(grandfather);//以g为中心左旋
						parent->_col = BLACK; //p变黑
						grandfather->_col = RED;//g变红
					}
					else //双旋
					{
						rotateR(parent);//先以p为中心右旋
						rotateL(grandfather);//再以g为中心左旋
						cur->_col = BLACK; //c变黑
						grandfather->_col = RED;//g变红
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return make_pair(Iterator(newnode, _root), true);
	}

	void rotateR(Node* parent) //右单旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR) //防止对空指针解引用
			subLR->_parent = parent;

		Node* pParent = parent->_parent; //先记录旋转前parent的父节点

		subL->_right = parent;
		parent->_parent = subL;
		if (pParent == nullptr) //旋转前parent为根节点
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else //旋转前parent不为根节点
		{
			subL->_parent = pParent;
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}
		}
	}

	void rotateL(Node* parent) //左旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* pParent = parent->_parent; //记录parent的_parent
		subR->_left = parent;
		parent->_parent = subR;

		if (pParent == nullptr)//更新前的parent是整棵树根节点
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			subR->_parent = pParent;

			if (pParent->_left == parent)
				pParent->_left = subR;
			else
				pParent->_right = subR;
		}
	}

	//Node* Find(const K& key)
	//{
	//	Node* cur = _root;
	//	while (cur)
	//	{
	//		if (cur->_kv.first < key)
	//		{
	//			cur = cur->_right;
	//		}
	//		else if (cur->_kv.first > key)
	//		{
	//			cur = cur->_left;
	//		}
	//		else
	//		{
	//			return cur;
	//		}
	//	}
	//	return nullptr;
	//}

	void Inoder() //中序遍历
	{
		_Inorder(_root);
		cout << endl;
	}
	
	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

private:
	void _Inorder(const Node* root)
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		//cout << root->_kv.first << ":" << root->_kv.second << ' ';
		_Inorder(root->_right);
	}

	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}
	Node* _root = nullptr;

};